package model

import (
	"errors"
	"fmt"
	"github.com/jmoiron/sqlx"
	"time"
	"zimuzu_cvn_web_api/config"
	. "zimuzu_cvn_web_api/pkg/e"
	"zimuzu_cvn_web_api/pkg/errno"
)

type WeeklyInfo struct {
	Id          int    `json:"id" db:"id" form:"id"`
	Phase       int    `json:"phase" db:"phase" form:"phase" binding:"required"`
	Url         string `json:"url" db:"url" form:"url" binding:"required"`
	Pic         string `json:"pic" db:"pic" form:"pic" binding:"required"`
	PublishTime string `json:"publishTime" db:"publish_time" form:"publish_time" binding:"required"`
	CreateTime  string `json:"create_time" db:"create_time"`
	UdateTime   string `json:"update_time" db:"update_time"`
}

type NewsInfo struct {
	Id          int    `json:"id" db:"id" form:"id"`
	Language    string `json:"language" db:"language" form:"language" binding:"required"`
	Url         string `json:"url" db:"url" form:"url" binding:"required"`
	Pic         string `json:"pic" db:"pic" form:"pic" binding:"required"`
	Title       string `json:"title" db:"title" form:"title" binding:"required"`
	Description string `json:"description" db:"description" form:"description" binding:"required"`
	CreateTime  string `json:"createTime" db:"create_time"`
}

func GetWeeklyListFromDB(db sqlx.Queryer, per, page int) (res []WeeklyInfo, err error) {
	start := (page - 1) * per
	err = sqlx.Select(db, &res, "select id, url, CONCAT('"+config.C.Domains.ResourcePic+"', pic) as pic, phase, from_unixtime(create_time, '%Y/%m/%d %H:%i:%s') as create_time, from_unixtime(update_time, '%Y/%m/%d %H:%i:%s') as update_time, date_format(publish_time, '%Y/%m/%d') as publish_time from cvn_weekly where status != 1 order by id desc limit ?,?", start, per)
	if err != nil {
		Log("info", "项目周报查询失败！", err.Error())
		return nil, err
	}

	return res, nil
}

func GetNewsListFromDB(db sqlx.Queryer, per, page int, language string) (res []NewsInfo, err error) {
	start := (page - 1) * per
	sqlStr := fmt.Sprintf("select id, language, url, CONCAT('" + config.C.Domains.ResourcePic + "', pic) as pic, title, description, from_unixtime(create_time, '%%Y/%%m/%%d') as create_time from cvn_news WHERE status != 1 ")
	if len(language) > 0 {
		sqlStr += fmt.Sprintf("AND language = '%s' ", language)
	}
	sqlStr += fmt.Sprintf("order by id desc limit %d,%d", start, per)
	err = sqlx.Select(db, &res, sqlStr)
	if err != nil {
		Log("info", "新闻报道查询失败！", err.Error())
		return nil, err
	}

	return res, nil
}

//新增新闻
func AddNewNews(params NewsInfo) (err error) {
	res := sqlx.MustExec(config.C.DB.DB, "INSERT INTO cvn_news SET `language` = ?, url = ?, pic = ?, title = ?, description = ?, create_time = ?",
		params.Language, params.Url, params.Pic, params.Title, params.Description, time.Now().Unix())
	_, err = res.LastInsertId()
	if err != nil {
		Log("err", err.Error())
		return errno.DbError
	}
	return
}

//更新新闻
func UpdateNews(params NewsInfo) (err error) {
	if params.Id <= 0 {
		return errors.New("id不能为空")
	}
	res := sqlx.MustExec(config.C.DB.DB, "UPDATE cvn_news SET `language` = ?, url = ?, pic = ?, title = ?, description = ?, update_time = ? WHERE id = ?",
		params.Language, params.Url, params.Pic, params.Title, params.Description, time.Now().Unix(), params.Id)
	_, err = res.RowsAffected()
	if err != nil {
		Log("err", err.Error())
		return errno.DbError
	}
	return
}

//删除新闻
func DeleteNews(id int) (err error) {
	res := sqlx.MustExec(config.C.DB.DB, "UPDATE cvn_news SET `status` = 1 WHERE id = ?", id)
	_, err = res.RowsAffected()
	if err != nil {
		Log("err", err.Error())
		return
	}
	return
}

//新增周报
func AddNewWeekly(params WeeklyInfo) (err error) {
	res := sqlx.MustExec(config.C.DB.DB, "INSERT INTO cvn_weekly SET `phase` = ?, url = ?, pic = ?, publish_time = ?, create_time = ?",
		params.Phase, params.Url, params.Pic, params.PublishTime, time.Now().Unix())
	_, err = res.LastInsertId()
	if err != nil {
		Log("err", err.Error())
		return errno.DbError
	}
	return
}

//更新周报
func UpdateWeekly(params WeeklyInfo) (err error) {
	if params.Id <= 0 {
		return errors.New("id不能为空")
	}
	res := sqlx.MustExec(config.C.DB.DB, "UPDATE cvn_weekly SET `phase` = ?, url = ?, pic = ?, publish_time = ?, update_time = ? WHERE id = ?",
		params.Phase, params.Url, params.Pic, params.PublishTime, time.Now().Unix(), params.Id)
	_, err = res.RowsAffected()
	if err != nil {
		Log("err", err.Error())
		return errno.DbError
	}
	return
}

//删除周报
func DeleteWeekly(id int) (err error) {
	res := sqlx.MustExec(config.C.DB.DB, "UPDATE cvn_weekly SET `status` = 1 WHERE id = ?", id)
	_, err = res.RowsAffected()
	if err != nil {
		Log("err", err.Error())
		return
	}
	return
}
